package com.pay.game.central.web.service;

import com.pay.game.central.web.bean.Directory;
import com.pay.game.central.web.mapper.DirectoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DirectoryService {

    private final String KEY_THRESHOLD = "THRESHOLD";
    private final String KEY_STORE_ORDER_LIMIT_DEFAULT = "STORE_ORDER_LIMIT_DEFAULT";
    private final String KEY_GOODS_TIME_INTERVAL = "GOODS_TIME_INTERVAL";
    private final String KEY_RUN_RANGE_LT = "run.range.lt";
    private final String KEY_ORDER_TIMEOUT = "order.timeout";

    @Autowired
    private DirectoryMapper mapper;

    private void setKey(String key, String value){
        Directory directory = mapper.getValue(key);
        if (directory!=null){
            directory.setValue(value);
            mapper.update(directory);
        }else {
            directory = new Directory();
            directory.setKey(key);
            directory.setValue(value);
            mapper.add(directory);
        }
    }

    /**
     * @return 商店下单超出阈值
     */
    public int getThreshold() {
        Directory directory = mapper.getValue(KEY_THRESHOLD);
        if (directory !=null )
            return Integer.parseInt(directory.getValue());
        else
            return 10;
    }

    /**
     * 设置店铺下单金额超出阈值
     */
    public void setThreshold(int threshold) {
        setKey(KEY_THRESHOLD, threshold + "");
    }

    /**
     * @return 商店默认下单数上限
     */
    public void setStoreOrderLimitDefault(int storeOrderLimitDefault){
        setKey(KEY_STORE_ORDER_LIMIT_DEFAULT, storeOrderLimitDefault + "");
    }

    /**
     * @return 商店默认下单数上限
     */
    public int getStoreOrderLimitDefault() {
        Directory directory = mapper.getValue(KEY_STORE_ORDER_LIMIT_DEFAULT);
        if (directory !=null )
            return Integer.parseInt(directory.getValue());
        else
            return 0;
    }


    /**
     * 设置店铺两次下单最小时间间隔
     */
    public void setGoodsTimeInterval(int storeTimeInterval){
        setKey(KEY_GOODS_TIME_INTERVAL, storeTimeInterval + "");
    }

    /**
     *
     * @return 店铺两次下单最小时间间隔
     */
    public int getGoodsTimeInterval() {
        Directory directory = mapper.getValue(KEY_GOODS_TIME_INTERVAL);
        if (directory !=null )
            return Integer.parseInt(directory.getValue());
        else
            return 0;
    }

    /**
     *
     * @return 商品价格可小于的值
     */
    public int getRunRangeLt(){
        Directory directory = mapper.getValue(KEY_RUN_RANGE_LT);
        if (directory !=null )
            return Integer.parseInt(directory.getValue());
        else
            return 0;
    }

    public void setRunRangeLt(int value){
        setKey(KEY_RUN_RANGE_LT, value + "");
    }


    /**
     *
     * @return 订单超时时间
     */
    public int getOrderTimeout(){
        Directory directory = mapper.getValue(KEY_ORDER_TIMEOUT);
        if (directory !=null )
            return Integer.parseInt(directory.getValue());
        else
            return 0;
    }

    public void setOrderTimeout(int value){
        setKey(KEY_ORDER_TIMEOUT, value + "");
    }

}
